package com.netease.nim.demo.common.util.file;

import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;

import com.netease.nim.demo.NimCache;
import com.netease.nim.demo.common.util.string.StringUtil;
import com.shwread.android.qysw10000204.R;

import java.util.Locale;

public class FileUtil {
	private static final String TAG = "FileUtil";
	
	// 获取文件扩展名
	public static String getExtensionName(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int dot = filename.lastIndexOf('.');
			if ((dot > -1) && (dot < (filename.length() - 1))) {
				return filename.substring(dot + 1);
			}
		}
		return "";
	}

	// 获取文件名
	public static String getFileNameFromPath(String filepath) {
		if ((filepath != null) && (filepath.length() > 0)) {
			int sep = filepath.lastIndexOf('/');
			if ((sep > -1) && (sep < filepath.length() - 1)) {
				return filepath.substring(sep + 1);
			}
		}
		return filepath;
	}
	
	// 获取不带扩展名的文件名
	public static String getFileNameNoEx(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int dot = filename.lastIndexOf('.');
			if ((dot > -1) && (dot < (filename.length()))) {
				return filename.substring(0, dot);
			}
		}
		return filename;
	}
	
	public static String getMimeType(String filePath) {
		if (TextUtils.isEmpty(filePath)) {
			return "";
		}
		String type = null;
		String extension = getExtensionName(filePath.toLowerCase());
		if (extension != null) {
			MimeTypeMap mime = MimeTypeMap.getSingleton();
			type = mime.getMimeTypeFromExtension(extension);
		}
		Log.i(TAG, "url:" + filePath + " " + "type:" + type);

		// FIXME
		if (StringUtil.isEmpty(type) && filePath.endsWith("aac")) {
			type = "audio/aac";
		}

		return type;
	}
	
	public enum SizeUnit {
        Byte,
        KB,
        MB,
        GB,
        TB,
        Auto,
    }
	
	public static String formatFileSize(long size) {
        return formatFileSize(size, SizeUnit.Auto);
    }

    public static String formatFileSize(long size, SizeUnit unit) {
		if (size < 0) {
			return NimCache.getContext().getString(R.string.unknow_size);
		}

		final double KB = 1024;
		final double MB = KB * 1024;
		final double GB = MB * 1024;
		final double TB = GB * 1024;
		if (unit == SizeUnit.Auto) {
			if (size < KB) {
				unit = SizeUnit.Byte;
			} else if (size < MB) {
				unit = SizeUnit.KB;
			} else if (size < GB) {
				unit = SizeUnit.MB;
			} else if (size < TB) {
				unit = SizeUnit.GB;
			} else {
				unit = SizeUnit.TB;
			}
		}

		switch (unit) {
		case Byte:
			return size + "B";
		case KB:
			return String.format(Locale.US, "%.2fKB", size / KB);
		case MB:
			return String.format(Locale.US, "%.2fMB", size / MB);
		case GB:
			return String.format(Locale.US, "%.2fGB", size / GB);
		case TB:
			return String.format(Locale.US, "%.2fPB", size / TB);
		default:
			return size + "B";
		}
	}
}
